/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.facet;

import com.intellij.openapi.options.ConfigurationException;
import com.googlecode.sca4idea.SCABundle;
import com.googlecode.sca4idea.providers.ProviderConfigurationEditor;
import com.googlecode.sca4idea.providers.SCAProvider;
import com.googlecode.sca4idea.providers.SCAProviderRegistry;
import org.jetbrains.annotations.Nls;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Panel used for providing the user interface for configuring a facet.
 */
public class FacetEditorPanel {
    private final SCAFacetConfiguration configuration;
    private JPanel mainPanel;
    private JComboBox scaProviderComboBox;
    private JButton configureProviderButton;

    public FacetEditorPanel(SCAFacetConfiguration configuration) {
        this.configuration = configuration;

        configureProviderButton.setVisible(false);
        configureProviderButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                ProviderConfigurationEditor providerEditor =
                        new ProviderConfigurationEditor(JOptionPane.getFrameForComponent(mainPanel),
                                ((SCAProviderListItem) scaProviderComboBox.getSelectedItem()).getProvider());
                providerEditor.pack();
                providerEditor.setLocationRelativeTo(JOptionPane.getFrameForComponent(mainPanel));
                providerEditor.setVisible(true);
            }
        });
    }

    @Nls
    public String getDisplayName() {
        return SCABundle.message("service.component.architecture.title");
    }

    public boolean isModified() {
        return !configuration.getSelectedProvider().equals(
                ((SCAProviderListItem) scaProviderComboBox.getSelectedItem()).getProvider());
    }

    public void apply() throws ConfigurationException {
        // Store the selected provider
        configuration.setSelectedProvider(
                ((SCAProviderListItem) scaProviderComboBox.getSelectedItem()).getProvider());
    }

    public void reset() {
        // Populate the SCA providers
        scaProviderComboBox.removeAll();
        SCAProvider[] providers = SCAProviderRegistry.getInstance().getAllProviders();
        for (SCAProvider provider : providers) {
            scaProviderComboBox.addItem(new SCAProviderListItem(provider));
            if (provider.equals(configuration.getSelectedProvider())) {
                scaProviderComboBox.setSelectedIndex(scaProviderComboBox.getModel().getSize() - 1);
            }
        }
    }

    public void disposeUIResources() {
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }
}
